<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="trainingRecordForm" :label-width="formLabelWidth" ref="refTrainingRecordForm" :rules="rules">
        <el-row>
          <el-col :span="11">
            <el-form-item label="员工姓名" prop="employeeName">
              <el-input v-model="trainingRecordForm.employeeName" placeholder="请选择员工姓名" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findEmployee"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="培训名称" prop="trainingName">
              <el-input v-model="trainingRecordForm.trainingName" placeholder="请输入培训名称" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="培训日期" prop="trainingDate">
              <el-date-picker
                  v-model="trainingRecordForm.trainingDate" type="date" placeholder="培训日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="培训时长" prop="durationHours">
              <el-input-number v-model="trainingRecordForm.durationHours" :min="1" :max="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="培训供应商" prop="provider">
              <el-input v-model="trainingRecordForm.provider" placeholder="请输入培训供应商" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="培训内容" prop="trainingDescription">
              <el-input type="textarea" :rows="3" v-model="trainingRecordForm.trainingDescription"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="employeeSelectorVisible" v-if="employeeSelectorVisible" title="员工基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <employee-selector @employee-selected-event="handleEmployeeSelectedEvent">
      </employee-selector>
    </el-dialog>
  </div>
</template>

<script>
import trainingRecordApi from '@/api/trainingRecordApi'
import EmployeeSelector from "@/views/employee/EmployeeSelector.vue";
export default {
  name: 'TrainingRecordAdd',
  components: {EmployeeSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      trainingRecordForm: {
        employeeId: '',
        employeeName: '',
        trainingName: '',
        trainingDate: '',
        durationHours: '',
        provider: '',
        trainingDescription: ''
      },
      rules: {
        employeeName: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        trainingName: [
          { required: true, message: '请输入培训名称', trigger: 'blur' }
        ],
        trainingDate: [
          { required: true, message: '请输入培训日期', trigger: 'blur' }
        ],
        durationHours: [
          { required: true, message: '请输入培训时长', trigger: 'blur' }
        ],
        provider: [
          { required: true, message: '请输入培训供应商', trigger: 'blur' }
        ],
        trainingDescription: [
          { required: true, message: '请输入培训内容', trigger: 'blur' }
        ],
      },
      employeeSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      trainingRecordApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.trainingRecordForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.trainingRecordForm = {
        employeeId: '',
        employeeName: '',
        trainingName: '',
        trainingDate: '',
        durationHours: '',
        provider: '',
        trainingDescription: ''
      }
      this.$emit('reset-training-record-add-event')
    },
    onSave () {
      this.$refs.refTrainingRecordForm.validate((valid) => {
        if (valid) {
          trainingRecordApi.save(this.trainingRecordForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-training-record-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findEmployee () {
      this.employeeSelectorVisible = true
    },
    handleEmployeeSelectedEvent (selectedEmployee) {
      this.employeeSelectorVisible = false
      if (selectedEmployee && 'employeeId' in selectedEmployee && this.trainingRecordForm.employeeId !== selectedEmployee['employeeId']) {
        this.trainingRecordForm.employeeId = selectedEmployee['employeeId']
        this.trainingRecordForm.employeeName = selectedEmployee['employeeName']
      }
    },
    onBack () {
      this.$emit('close-training-record-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>